﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Nest;
using ES.Model;

namespace ES.Client.Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            EventSearch();
            //SearchArticles();
            //AssociatorSearch();
            //ContactDailyIncreasementReport();
            Console.ReadKey();
        }

        static void SearchJoins()
        {
            ElasticHub<HYEventJoin> hub = new ElasticHub<HYEventJoin>();
            var parameter = hub.NewParameter();
            parameter.Must.IN("ExtraFieldsForES.String_Field648413079", new List<string>() { "多选3" });
            var response = hub.Search(parameter);
            var docs = response.Documents;
        }

        static void SearchArticles()
        {
            ElasticHub<Article> hub = new ElasticHub<Article>();
            var parameter = hub.NewParameter();
            parameter.Highlighter.Fields(f => f.Field(new Field("*")).PreTags("<baidu>").PostTags("</baidu>"));
            parameter.Must.Like("Name", "李彦宏").GT(a => a.ArticleId, 878754545584494);
            //parameter.Source.Includes(t => t.Fields(f => f.Content, f => f.Description));
            //parameter.Source.Excludes(t => t.Fields(f => f.Content, f => f.Description));
            parameter.Sorter.Descending(t => t.ArticleId);
            var response = hub.Search(parameter);
            if (response.IsValid)
            {
                var documents = response.GetHighlightedDocuments();
                foreach (var doc in documents)
                {
                    Console.WriteLine(doc.Content + "-------------" + doc.Description);
                }
            }
        }

        static void EventSearch()
        {
            var keyword = "姚世荣";
            var elastic = new ElasticHub<HYEvent>();
            var parameter = elastic.NewParameter();
            parameter.Must.EQ("IsDeleted", false).Like(e => e.Title, keyword);
            parameter.Sorter.Descending(t => t.SubType);
            parameter.Size = 10000;
            parameter.From = 0;
            Console.WriteLine("------------------start--------------------");
            var response = elastic.Search(parameter);
            if (response.IsValid)
            {
                Console.WriteLine(response.Total);
                var data = response.Documents.ToList();
                foreach (var item in data)
                {
                    Console.WriteLine(item.Title);
                }
            }
            Console.WriteLine("------------------finish--------------------");
        }

        static void AssociatorSearch()
        {
            var elastic = new ElasticHub<Associator>();
            var parameter = elastic.NewParameter();
            parameter.Highlighter.Fields(
                f => f.Field(a => a.RealName).PreTags("<RealName>").PostTags("</RealName>"),
                f => f.Field(a => a.Company).PreTags("<Company>").PostTags("</Company>"));
            parameter.Must.EQ("IsDeleted", false).FullText("王");
            parameter.Should.Like(a => a.RealName, "王").Like(a => a.Company, "31会");
            parameter.Size = 100;

            var response = elastic.Search(parameter);
            if (response.IsValid)
            {
                var data = response.GetHighlightedDocuments();
                foreach (var item in data)
                {
                    Console.WriteLine(item.AssociatorId + "----" + item.RealName + "---" + item.Company);
                }
            }

            Console.WriteLine("finish");
        }

        static void ContactDailyIncreasementReport()
        {
            ElasticHub<Contact> hub = new ElasticHub<Contact>();
            QueryParameter<Contact> parameter = hub.NewParameter();
            parameter.Must.EQ("CompanyId", 281111784);
            parameter.Must.FullText("测试");
            parameter.Size = 1000000;
            var response = hub.Search(parameter);
            if (response.IsValid)
            {
                var terms = response.Documents;
            }
        }

        static void MapContact()
        {
            var elastic = new ElasticHub<Contact>();
            var scrmdbResponse = elastic.Client.CreateIndex(elastic.Index, c => c.Mappings(s =>
                s.Map<Contact>(dd => dd.AutoMap().DateDetection(false).DynamicTemplates(t => t.
                    DynamicTemplate("ExtraFieldsForES_Int", td => td.PathMatch("ExtraFieldsForES.Int_*").Mapping(m => m.Number(p => p.Type(NumberType.Integer).IgnoreMalformed(true)))).
                    DynamicTemplate("ExtraFieldsForES_Double", td => td.PathMatch("ExtraFieldsForES.Double_*").Mapping(m => m.Number(p => p.Type(NumberType.Double).IgnoreMalformed(true)))).
                    DynamicTemplate("ExtraFieldsForES_Bool", td => td.PathMatch("ExtraFieldsForES.Bool_*").Mapping(m => m.Boolean(p => p.Index(true)))).
                    DynamicTemplate("ExtraFieldsForES_String", td => td.PathMatch("ExtraFieldsForES.String_*").Mapping(m => m.Keyword(p => p.Index(true)))).
                    DynamicTemplate("ExtraFieldsForES_Date", td => td.PathMatch("ExtraFieldsForES.Date_*").Mapping(m => m.Date(p => p.Format("yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd HH:mm:ss||yyyy/MM/dd HH:mm:ss||strict_date_optional_time").Index(true).IgnoreMalformed(true))))
                    ))));

            var scrmdb = scrmdbResponse.DebugInformation;
        }

        static void SearchTest()
        {
            var dt0 = DateTime.Now;

            var elastic = new ElasticHub<Contact>();
            var parameter = elastic.NewParameter();
            var conditionMust = parameter.Must;
            conditionMust.EQ("CompanyId", 104375386);
            conditionMust.EQ("IsDeleted", false);
            conditionMust.EQ(c => c.RealName, "肖骁");
            parameter.Size = 100;
            var response = elastic.Search(parameter);
            var ts = response.Took;
            var total = response.Total;
            var dt1 = DateTime.Now;
            var seconds = (dt1 - dt0).TotalSeconds;
            if (response.IsValid)
            {
                var contacts = response.Documents;
                var ss = contacts.Count();
                foreach (var item in contacts)
                {
                    Console.WriteLine(item.RealName + "---" + item.Email);
                }
            }

            Console.WriteLine("耗时：" + ts + "毫秒");
            Console.WriteLine("finish");
        }
    }
}
